package com.flashcards.vocabulary;

import android.util.Log;
import com.flashcards.Word;

import java.util.ArrayList;
import java.util.Iterator;

public class Vocabulary implements Iterable<Word> {
    private ArrayList<Word> words = new ArrayList<Word>();

    public Vocabulary() {
    }

    public Word next(String english) {
        if (words.isEmpty()) {
            return Word.NULL;
        }
        int index = words.indexOf(new Word(english, ""));
        index = index == words.size() - 1 ? index : index + 1;
        return words.get(index);
    }

    public Word previous(String english) {
        if (words.isEmpty()) {
            return Word.NULL;
        }
        int index = words.indexOf(new Word(english, ""));
        index = index == 0 ? 0 : index - 1;
        return words.get(index);
    }

    public Iterator<Word> iterator() {
        return words.iterator();
    }

    public int size() {
        return words.size();
    }

    public static Vocabulary createInstance() {
        return new RemoteVocabulary().lookUp();
    }

    public void add(Word word) {
        Log.i(">>>>", "add world (" + word.getEng() + "--" + word.getTranslation() + ")");
        word.addTo(this.words);
    }

    public Word first() {
        if (words.isEmpty()) {
            return Word.NULL;
        }
        return words.get(0);
    }
}
